$(document).ready(function () {
    fetchCommunicates();

});

function fetchCommunicates() {
    $('#communicate-list').bootstrapTable({
        method: 'get', 
        dataType: "json", 
        pageSize: 20,  
        pageNumber: 1, 
        toolbar: "#toolbar",
        sortName: "XID",
        sortOrder: "asc",
        clickToSelect: "true",
        sidePagination: "server",
        queryParamsType: "",
        queryParams: getParams,  
        pagination: true,
        pageList: "[20, All]",
        search: "true",
        url: "../Handle/ExchangeHandlerService.ashx?method=Get_ExchangeTheme_List",
        responseHandler: function (res) {
            res.total = res.RecordCount;
            //console.log(res);
            return res;
        },
        /*onClickRow: function (row) {
            //console.log(row);
            $("#msg-content").text(row.msg);
            $("#msg-time").text(row.CurrentDate);
            $("#CommunicateThemeModal").modal("show");
            $.ajax({
                type: "GET",
                data: { MessageID: row.id},
                url: "../Handle/SysMessageHandlerService.ashx?method=Mark_One_SysMsg_Readed",
                dataType: "json",
                success: function (data) {
                    if(1 != data.MessageType) {
                        alert("标记为已读报错，请联系管理员报告问题");
                    } else {
                        $("#communicate-list").bootstrapTable('refresh', {});
                    }
                }
            });
        }, */
        
    });
}

function getParams(params) {
    var temp = { 
        PageSize : params.pageSize, 
        PageIndex : params.pageNumber
    };
    return temp;
}

function cellStyle(value, row, index) {
    if (0 == row.flag) {
        return {
            classes: "text-bold"
        };
    }
    return {};
}

function actionFormatter(value, row, index) {
    var handClass = row.IsTop?"glyphicon-hand-up":"glyphicon-hand-down";
    var handTitle = row.IsTop?"取消置顶":"置顶";
    var eyeClass = row.IsHidden?"glyphicon-eye-close":"glyphicon-eye-open";
    var eyeTitle = row.IsHidden?"取消隐藏":"隐藏";
    
    return [
    '<a class="delete ml10" href="javascript:void(0)" title="删除">',
    '<i class="glyphicon glyphicon-trash"></i>',
    '</a> ',
    ' <a class="edit ml10" href="javascript:void(0)" title="编辑">',
    '<i class="glyphicon glyphicon-edit"></i>',
    '</a> ',
    ' <a class="cover ml10" href="javascript:void(0)" title="'+eyeTitle+'">',
    '<i class="glyphicon '+eyeClass+ '"></i>',
    '</a> ',
    ' <a class="top ml10" href="javascript:void(0)" title="'+handTitle+'">',
    '<i class="glyphicon '+handClass+ '"></i>',
    '</a>'
    ].join('');
}

function titleFormatter(value, row, index) {
    value=value?value:'';
    var length=value.length;
    var ellipsis = "";
    if(length&&length>15){
        length=15;
        ellipsis = "...";
    }
    return"<span title ='"+value+"'>"+value.substring(0,length)+ellipsis+"</span>"
}

function contentFormatter(value, row, index) {
    value=value?value:'';
    var length=value.length;
    var ellipsis = "";
    if(length&&length>20){
        length=20;
        ellipsis = "...";
    }
    return"<span title ='"+value+"'>"+value.substring(0,length)+ellipsis+"</span>"
}



window.actionEvents = {
	'click .delete': function (e, value, row, index) {
        alert('You click delete icon, row: ' + JSON.stringify(row));
        console.log(value, row, index);
        $("#modal-content").html("你确定删除吗--？");
        $("#myModal").modal("show");
    },
    'click .edit': function (e, value, row, index) {
        alert('You click edit icon, row: ' + JSON.stringify(row));
        console.log(value, row, index);
    },
    'click .cover': function (e, value, row, index) {
        // alert('You click edit hide, row: ' + JSON.stringify(row));
        // console.log(value, row, index);
        var returnStr = row.IsHidden?"取消隐藏成功！":"隐藏成功！";
        $.ajax({
            type: "POST",
            data: {ExchangeID: row.EXID},
            url: "../Handle/ExchangeHandlerService.ashx?method=ShieldORDisp_One_ExchangeTheme_By_EXID",
            dataType: "json",
            success: function (data) {
                //console.log(data);
                alert(returnStr);
                $('#communicate-list').bootstrapTable("refresh");
            }
        });
    },
    'click .top': function (e, value, row, index) {
        //alert('You click top icon, row: ' + JSON.stringify(row));
        // console.log(value, row, index);
        var returnStr = row.IsTop?"取消置顶成功！":"置顶成功！";
        $.ajax({
            type: "POST",
            data: {ExchangeID: row.EXID},
            url: "../Handle/ExchangeHandlerService.ashx?method=StickORCannel_One_ExchangeTheme_By_EXID",
            dataType: "json",
            success: function (data) {
                //console.log(data);
                alert(returnStr);
                $('#communicate-list').bootstrapTable("refresh");
            }
        });
    }
};